package intelligentcity.gui;

import intelligentcity.cities.AlabamaCityBuilder;
import intelligentcity.cities.ChicagoCityBuilder;
import intelligentcity.cities.KansasCityBuilder;
import intelligentcity.model.City;
import intelligentcity.model.Point;
import intelligentcity.model.traffic.Traffic;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GuiAlabama extends JPanel implements ActionListener {

    /**
	 * 
	 */
    private static final long serialVersionUID = -5817570028862315116L;

    private City city;
    private Traffic traffic;
    private boolean repaint = false;
    private Timer timer;
    private int timing = 100;
    
    public GuiAlabama() {
        timer = new Timer(timing, this);
        timer.start();
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("City");
        frame.add(new GuiAlabama());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(950, 750);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        g2d.translate(25, 5);

        if (!repaint) {
            
            city = new AlabamaCityBuilder().createNeighbourhood()
                    .createStartPoints(new Point(10, 10), new Point(800, 500)).build();

            
            traffic = new Traffic(city, 3);
            traffic.start();
            repaint = true;

        }

        CityDrawer.drawCity(g2d, city);
        TrafficDrawer.drawTraffic(g2d, traffic);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
            repaint();
    }

}
